package com.travel.server.manager.impl.dal;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.UserInfoDao;
import com.travel.server.manager.model.UserInfo;
import com.travel.server.manager.model.UserInfoExample;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/26 15:40
 * @Desc:
 */
@Component
@Slf4j
public class UserDal {

    @Autowired
    private UserInfoDao userInfoDao;

    public UserInfo select(String account, String password){
        UserParam param = new UserParam();
        param.setAccount(account);
        param.setPassword(password);

        List<UserInfo> list = selectByExample(param);

        return CollectionUtil.getFirst(list);
    }

    public UserInfo selectById(Integer id){
        UserInfo userInfo = userInfoDao.selectByPrimaryKey(id);
        if(userInfo == null){
            log.info("未查询到用户信息, id {}", id);
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }
        return userInfo;
    }

    public void insertOrUpdate(UserInfo userInfo){
        if(userInfo.getId() == null){
            userInfoDao.insertSelective(userInfo);
        }else{
            userInfoDao.updateByPrimaryKey(userInfo);
        }
    }

    public List<UserInfo> selectByExample(UserParam param){
        UserInfoExample example = makeExample(param);
        return userInfoDao.selectByExample(example);
    }

    public PageDto<UserInfo> select(UserParam param){
        UserInfoExample example = makeExample(param);
        int total = userInfoDao.countByExample(example);
        example.setOrderByClause("update_time desc");
        List<UserInfo> list = userInfoDao.selectByExample(example);
        PageDto<UserInfo> pageDto = new PageDto<>(list, total);
        return pageDto;
    }

    private UserInfoExample makeExample(UserParam param){
        UserInfoExample example = new UserInfoExample();
        UserInfoExample.Criteria criteria = example.or();
        if(StringUtils.isNotBlank(param.getName())){
            criteria.andNameLike("%" + param.getName() + "%");
        }
        if(StringUtils.isNotBlank(param.getRealName())){
            criteria.andNameEqualTo(param.getRealName());
        }
        if(param.getState() != null){
            criteria.andStateEqualTo(param.getState());
        }
        if(StringUtils.isNotBlank(param.getAccount())){
            criteria.andAccountEqualTo(param.getAccount());
        }
        if(StringUtils.isNotBlank(param.getPassword())){
            criteria.andPasswordEqualTo(param.getPassword());
        }
        if(param.getEnable() != null){
            criteria.andEnableEqualTo(param.getEnable());
        }
        if(param.getDepartmentId() != null){
            criteria.andDepartmentIdEqualTo(param.getDepartmentId());
        }
        if(param.getOffset() != null && param.getLimit() != null){
            example.setOffset(param.getOffset());
            example.setLimit(param.getLimit());
        }
        return example;
    }

    @Data
    public static class UserParam{
        private String realName;

        private String name;

        private Integer state = 1;

        private Integer offset;

        private Integer limit;

        private String account;

        private String password;

        private Integer enable;

        private Integer departmentId;
    }
}
